package com.lsh.oms.worker.service.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.core.enums.ReturnStatus;
import com.lsh.oms.core.enums.TaskStatus;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.model.order.OrderRoHead;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.proxy.service.EsProxy;
import com.lsh.oms.core.proxy.service.OfcProxy;
import com.lsh.oms.core.service.other.RoOrderService;
import com.lsh.oms.core.service.po.OmsTaskService;
import com.lsh.oms.worker.service.base.OmsSimpleElasticJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/2/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Slf4j
@Component
@Deprecated
public class PushOrderRo2ofcJop extends OmsSimpleElasticJob {

    public static String SUCCESS = "0000";

    @Autowired
    private RoOrderService roOrderService;

    @Autowired
    private OmsTaskService omsTaskService;

    @Autowired
    private EsProxy esProxy;

    @Autowired
    private OfcProxy ofcProxy;

    @Override
    protected Integer getFetchSize() {
        return 20;
    }

    @Override
    protected TaskType getTaskType() {
        return TaskType.PUSH_RO;
    }

    @Override
    protected List<Integer> dealTaskStatus() {

        List<Integer> taskStatusList = new ArrayList<>();

        taskStatusList.add(TaskStatus.TASK_UN_DEAL.getCode());
        taskStatusList.add(TaskStatus.TASK_DEAL_FAIL.getCode());

        return taskStatusList;
    }

    @Override
    protected int process(OmsTask omsTask) {

        OrderRoHead orderRoHead = this.roOrderService.getRoHeadByReturnOrderId(Long.valueOf(omsTask.getRefCode()), true);
        log.info(orderRoHead.getOrderId() + " 订单 ro 提交ofc 开始");
        CommonResult<List<String>> commonResult = ofcProxy.createRo(orderRoHead);
        log.info(orderRoHead.getOrderId() + " 订单 ro 提交ofc 结束, 结果 {}", JSON.toJSONString(commonResult));

        // TODO 判断是否成功
        if (!commonResult.getCode().equals(SUCCESS)) {
            return -1;
        }

        List<String> sobillCodeList = commonResult.getData();

        OrderRoHead update = new OrderRoHead();
        update.setReturnOrderId(orderRoHead.getReturnOrderId());
        update.setStatus(ReturnStatus.Submiting_So.getIndex());
        update.setSoReturnId(StringUtils.join(sobillCodeList, ","));

        if(roOrderService.setSoBillCode(update) > 0){

            esProxy.editIndex(update,"orderRoHead: " + orderRoHead.getReturnOrderId());

            OmsTask omsTask4Roquery = new OmsTask();

            omsTask4Roquery.setTaskType(TaskType.QUERY_RO_STATUS.getCode());
            omsTask4Roquery.setTaskStatus(TaskStatus.TASK_UN_DEAL.getCode());
            omsTask4Roquery.setRefCode(String.valueOf(orderRoHead.getReturnOrderId()));

            JSONObject extJsonObject = JSON.parseObject(orderRoHead.getExt());
            String asfId = extJsonObject.getString("asf_id");
            omsTask4Roquery.setContent(asfId);

            omsTaskService.add(omsTask4Roquery);
        }

        return 1;
    }
}
